package com.itheima.health.jobs;

import com.itheima.health.common.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

@Slf4j
public class ClearRedisJob {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 清理redia数据库中图片名Set集合中的数据
     * 集合:setmealPicResources
     *      setmealPicDbResources
     */
    public void clearRedisJob(){
        try(Jedis jedis = jedisPool.getResource()){
            //在setmealPicResources集合中删除交集数据
            Set<String> sinter = jedis.sinter(RedisConst.SETMEAL_PIC_RESOURCES, RedisConst.SETMEAL_PIC_DB_RESOURCES);
            log.info("[清理setmealPicResources中垃圾数据开始]");
            for (String img : sinter) {
                log.info("[清理setmealPicResources中垃圾数据]name:{}",img);
                jedis.srem(RedisConst.SETMEAL_PIC_RESOURCES,img);
            }
            log.info("[清理setmealPicResources中垃圾数据结束]");
            //删除setmealPicDbResources数据
            //Set<String> setmealPicDbResources = jedis.smembers(RedisConst.SETMEAL_PIC_DB_RESOURCES);
            log.info("[清理setmealPicDbResources中垃圾数据开始]");
            /*for (String setmealPicDbResource : setmealPicDbResources) {
                log.info("[清理setmealPicDbResources中垃圾数据]name:{}",setmealPicDbResource);
                jedis.srem(RedisConst.SETMEAL_PIC_DB_RESOURCES,setmealPicDbResource);
            }*/
            jedis.del(RedisConst.SETMEAL_PIC_DB_RESOURCES);
            log.info("[清理setmealPicDbResources中垃圾数据结束]");
        }
    }

}
